<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <comm-search @search="initList()" v-if="showSearch">
        <template #left>
          <el-form-item>
            <el-input v-model="formData.name" clearable placeholder="请输入表名" style="width: 200px;" class="filter-item" @keyup.enter.native="initList()" />
          </el-form-item>

        </template>
      </comm-search>

      <comm-tool  :selections="selections" v-model:show-search="showSearch" :show-add="false" :show-del="false" :show-edit="false"
                 :del-url="baseUrl" @refresh="initList()">
        <template #left>
          <el-button
            class="filter-item"
            type="success"
            :disabled="selections.length === 0"
            @click="syncTable"
          >同步</el-button>
        </template>

      </comm-tool>

    </div>
    <!--表格渲染-->
    <el-table ref="table" v-loading="loading" :data="dataList" style="width: 100%;" @selectionChange="selectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column :show-overflow-tooltip="true" prop="tableName" label="表名" />
      <el-table-column :show-overflow-tooltip="true" prop="engine" label="数据库引擎" />
      <el-table-column :show-overflow-tooltip="true" prop="coding" label="字符编码集" />
      <el-table-column :show-overflow-tooltip="true" prop="remark" label="备注" />
      <el-table-column prop="createTime" label="创建日期" />
      <el-table-column label="操作" width="160px" align="center" fixed="right">
        <template #default="scope">
          <el-button style="margin-left: -1px;margin-right: 2px" type="primary" link>
            <router-link :to="'/tools/generator/config/' + scope.row.tableName">
              配置
            </router-link>
          </el-button>
          <el-button type="primary" link style="margin-left: -1px" @click="toGen(scope.row.tableName)">生成</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination v-model:page="queryParam.page" v-model:limit="queryParam.size" :total="total" @change="initList()"/>
  </div>
</template>

<script setup>
import {toRefs} from 'vue'
import Pagination from '@/components/Pagination'
import mixins from "@/mixins/mixins";
import CommSearch from "@/components/Comm/CommSearch.vue";
import CommTool from "@/components/Comm/CommTool.vue";
import { ElMessage } from 'element-plus'
import {post} from "@/utils/request";


const {initList, total, dataList, data,selectionChange} = mixins()
const {queryParam, loading,  showSearch, baseUrl,formData,selections} = toRefs(data)

baseUrl.value = '/generator/tables'

function  toGen(tableName) {
  // 生成代码
  post('/generator/'+tableName+'/0').then(data => {
    ElMessage({
      message: '生成成功',
      type: 'success',
    })

  })
}
function  syncTable() {
  const tables = []
  selections.value.forEach(val => {
    tables.push(val.tableName)
  })
  post('/generator/sync',tables).then(() => {
    ElMessage({
      message: '同步成功',
      type: 'success',
    })
    initList(null)
  }).then(() => {

  })
}

initList(null)
</script>


